package bookstore.servlet;

import bookstore.utils.AuthenticateException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;


@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
    @ExceptionHandler(AuthenticateException.class)
    public ResponseEntity handelAuthenticateException(AuthenticateException e) {
        String msg = e.getType() == AuthenticateException.AuthType.ADMIN ? "admin" : "login";
        return new ResponseEntity(msg, HttpStatus.UNAUTHORIZED);
    }
}
